function [result, Ib ] = character_Segmentation4( img, IS_DEBUG, ~,~ )
%CHARACTER_SEGMENTATION Summary of this function goes here
%   Detailed explanation goes here
z = size(img,3);
if z==3
    img = rgb2gray(img);
end
% temp = double(img);
% maxi = max(max(temp))*1.0;
% tempimg = temp/maxi;
% tempimg = 2.^(-tempimg);

if IS_DEBUG
    figure(1);
    subplot(2,1,1);imshow(img);
end


xmin= 5;
ymin= 5;
maxwidth = 256;
img=imresize(img,[NaN maxwidth]);

[maxhigh, maxwidth] = size(img);
width = maxwidth - 2*xmin;
high = maxhigh-2*ymin;
img = imcrop(img, [xmin ymin width high]);
Ib = ~Sauvola(img,[0.1 128 15]);
% figure(1);
% subplot(2,1,1);imshow(Ib);
% Ib = imdilate(Ib,strel('square',2));
% subplot(2,1,2);;imshow(Ib);
Ib = bwareaopen(Ib, 200);

cost = 5;
img_inv = Ib';
[~, vector] = characterSegment.breakCost(img_inv, cost);
[ arrLineO, arrLine] = characterSegment.getLine(vector>cost, img_inv, img');


result = {};
% resulttemp = {};
numResult = 0;
for iLine = 1:length(arrLine)
    aSegment = arrLine{iLine};
    
    %     newim = padarray(aSegment, [5 5], 0, 'both');
    %
    %
    %
    %     SE = ones(1,3);
    %     newim = imclose(newim, SE);
    %     SE = ones(3,1);
    %     newim = imclose(newim, SE);
    %     SE = ones(1,2);
    %     newim = imopen(newim, SE);
    %     SE = ones(2,1);
    %     newim = imopen(newim, SE);
    %     [h, w] = size(newim);
    %     newim = imcrop(newim, [6 6 (w-11) (h-11)]);
    
    %         fill hole
    tempim = bwareaopen(~aSegment, 100);
    tempim = ~tempim;
    aSegment = aSegment|tempim;
    
    cost = 5;
    [out, ~] = characterSegment.breakCost(aSegment, cost);
    out = bwareaopen(out, 100);
    cost = 2;
    [out, ~] = characterSegment.breakCost(out, cost);
   
    arrCharacter = characterSegment.getCharacter( out, arrLineO{iLine}, IS_DEBUG);
    
    for i=1: length(arrCharacter)
       
        numResult = numResult +1;
        result{numResult}= arrCharacter{i};
    end
    
end


% highO = zeros(1,length(resulttemp));
% for i=1:length(resulttemp)
%     highO(1,i) = size(resulttemp{i},1);
% end
% meanValue = mean(highO);
% num = 0;
% for i=1:length(resulttemp)
%     if (highO(1,i) > (meanValue -10)) && (highO(1,i) < (meanValue +10))
%         tempim = resulttemp{i};
%         w = size(tempim,2);
%         if(w<16)
%             tempim= imresize(tempim, [64 NaN]);
%             p1=floor((64-w)/2);
%             p2=64-w-p1;
%             tempim = padarray(tempim, [0 p1], 0, 'pre');
%             if p2>0
%                 tempim = padarray(tempim, [0 p2], 0, 'post');
%             end
%         end
%         tempim= imresize(tempim, [64 64]);
%         
% 
%         num = num +1;
%         result{num} = tempim;
%     end
% end

if IS_DEBUG
    numCharacter = numResult;
    col = ceil(sqrt(numCharacter));
    row = ceil(numCharacter/col);
    figure(2);
    for i=1:numCharacter
        subplot(row, col, i);
        imshow(result{i});
    end

end
end

